cur_file="$(cd "$(dirname "$0")"; pwd)/${0##*/}"
while [[ -h "$cur_file" ]]; do
  cur_file="$(readlink "$cur_file")"
done
cur_dir="$(dirname "$cur_file")"

cd "$cur_dir"

echo_err() { echo $'\e[0;31m'"$1"$'\e[0m'; }
echo_info() { echo $'\e[0;32m'"$1"$'\e[0m'; }
echo_warn() { echo $'\e[0;33m'"$1"$'\e[0m'; }
printf_err() { printf $'\e[0;31m'"$1"$'\e[0m'; }
printf_info() { printf $'\e[0;32m'"$1"$'\e[0m'; }
printf_warn() { printf $'\e[0;33m'"$1"$'\e[0m'; }

cd "$cur_dir"

version="5.0.10"

build(){
  [[ "$(uname)" =~ ^(MINGW|MSYS) ]] && exe=".exe"
  test -d dist || mkdir dist
  rm -rf dist/*
  cp redis-5.0.10/src/redis-benchmark$exe dist
  cp redis-5.0.10/src/redis-check-aof$exe dist
  cp redis-5.0.10/src/redis-check-rdb$exe dist
  cp redis-5.0.10/src/redis-cli$exe dist
  cp redis-5.0.10/src/redis-sentinel$exe dist
  cp redis-5.0.10/src/redis-server$exe dist
  cp redis-5.0.10/redis.conf dist
  if [ -n "$exe" ]; then
    cp /usr/bin/msys-2.0.dll dist
  fi
}

case "$1" in
  build)
    make
    build
    if [ -n "$exe" ]; then
      tar -zcvf redis-$version-win.tgz dist/* --transform=s/^dist//
    else
      tar -zcvf redis-$version.tgz dist/* --transform=s/^dist//
    fi
  ;;
  clean)
    rm -rf dist
    rm -rf redis-*.tgz
    make distclean
  ;;
  *)
  ;;
esac
